package com.bq.gametrade.adapter;

import android.content.Context;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bq.gametrade.R;
import com.bq.gametrade.bean.AddressBookDetails;
import com.bq.gametrade.utils.SharedPreferencesUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

/**
 * Created by Feel on 2016/12/27.
 */

public class AddressBookListAdapter extends BaseAdapter {

    private ArrayList<AddressBookDetails> mDatas;
    private Context context;
    private  String friendLists;

    public AddressBookListAdapter(Context context, ArrayList<AddressBookDetails> list) {
        this.mDatas = list;
        this.context = context;
         friendLists = (String) SharedPreferencesUtils.getValue(context, SharedPreferencesUtils.friend_lists, "");
    }

    public void setData(ArrayList<AddressBookDetails> list) {
        this.mDatas = list;

    }


    /**
     * ItemClick的回调接口
     *
     * @author feel
     */
    public interface OnAddFriendListener {
        void onAdd(int position);
    }

    private OnAddFriendListener mOnItemClickListener;

    public void setOnItemClickLitener(OnAddFriendListener onAddFriendListener) {
        this.mOnItemClickListener = onAddFriendListener;
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.listview_addressbooklist_item, null);
            viewHolder.tvFriendNickName = (TextView) view.findViewById(R.id.tv_friendNickName);
            viewHolder.tvFriendPhoneNumber = (TextView) view.findViewById(R.id.tv_friendPhoneNumber);
            viewHolder.ivHeadImage = (ImageView) view.findViewById(R.id.civ_friendHeadImage);
            viewHolder.btnAdd = (Button) view.findViewById(R.id.btn_add);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        AddressBookDetails details = mDatas.get(position);
        if (details.userName.equals("")) {
            viewHolder.tvFriendNickName.setText(details.bqAccount);
            viewHolder.tvFriendNickName.setTextColor(context.getResources().getColor(R.color.color666666));
        } else {
            viewHolder.tvFriendNickName.setText(new String(Base64.decode(details.userName.getBytes(), 0, details.userName.length(), Base64.DEFAULT)));
        }
        viewHolder.tvFriendPhoneNumber.setText(details.userId);
        Glide.with(context).load(details.headimg).error(R.mipmap.user_headimg_gray).into(viewHolder.ivHeadImage);

        if (!friendLists.equals("") && friendLists.contains(details.userId)) {
            viewHolder.btnAdd.setClickable(false);
            viewHolder.btnAdd.setBackgroundResource(R.color.gray);
            viewHolder.btnAdd.setText("已添加");
        } else {
            viewHolder.btnAdd.setBackgroundResource(R.color.blue_108ee9);
            viewHolder.btnAdd.setText("添加");
            viewHolder.btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onAdd(position);
                }
            });
        }


        return view;
    }


    class ViewHolder {
        TextView tvFriendNickName;
        TextView tvFriendPhoneNumber;
        ImageView ivHeadImage;
        Button btnAdd;

    }
}
